<?php

class Grid_model extends CI_Model{
	public function pegawai_dropdown($k="nip",$v="nip",$default="-- Pilih Pegawai --",$add_txt_field='nama')
	{
		if(is_string($default))
			$data = array(''=>$default);
		else if($default)
			$data = array(''=>'-- Pilih Pegawai --');
		else
			$data = array();
		
		$rs = $this->db->select("$k as k,$v as v" . (is_string($add_txt_field) ? ", $add_txt_field as ext":'') )
				 ->get('pegawai')
				 ->result_object();
		foreach ($rs as $r) {
			$data[$r->k] = $r->v . (is_string($add_txt_field) ? ' / ' . $r->ext : '') ;
		}
		return $data;
	}

	public function pengguna_nip_exist($nip)
	{
		$nips = array();
		$r = $this->db->select('nip')->get('pengguna')->result_object();
		foreach ($r as $s) {
			$nips[] = $s->nip;
		}
		return $nips;
	}
	public function get_user_info($nip)
	{
		$r =  $this->db->select('b.id_group gid,
								 a.id_pengguna,a.status,
								 a.nip,a.nip username, 
								 a.password, 
								 b.group tipe_admin,
								 c.instansi,
								 c.id_pegawai,
								 c.nama nama_lengkap,
								 d.jabatan,
								 e.golongan,
								 f.nama nama_atasan,
								 f.id_jabatan id_jabatan_atasan,
								 f.nip nip_atasan,
								 f.id_pegawai id_atasan,
								 g.jabatan jabatan_atasan')
									 ->join('group_pengguna b','b.id_group = a.id_group','left')
									 ->join('pegawai c','c.nip=a.nip','left')
									 ->join('jabatan d','d.id_jabatan=c.id_jabatan','left')
									 ->join('golongan e','e.id_golongan=c.id_golongan','left')
									 ->join('pegawai f','f.nip=c.atasan','left')
									 ->join('jabatan g','g.id_jabatan=f.id_jabatan','left')
									 ->where(array('a.nip'=>$nip))
									 ->get('pengguna a')->row_array();

		foreach ($r as $key => $value) {
			if(empty($value))
				$r[$key] = '-';
		}
		return $r;
	}
	public function get_pegawai_info($nip)
	{
		$r =  $this->db->select('a.nip,a.instansi,a.nama ,d.jabatan,e.golongan,f.nama nama_atasan,f.nip nip_atasan,f.id_jabatan id_jabatan_atasan,g.jabatan jabatan_atasan')
									 //->join('group_pengguna b','b.id_group = a.id_group','left')
									 //->join('pegawai c','c.nip=a.nip','left')
									 ->join('jabatan d','d.id_jabatan=a.id_jabatan','left')
									 ->join('golongan e','e.id_golongan=a.id_golongan','left')
									 ->join('pegawai f','f.nip=a.atasan','left')
									 ->join('jabatan g','g.id_jabatan=f.id_jabatan','left')
									 ->where(array('a.nip'=>$nip))
									 ->get('pegawai a')->row_array();

		foreach ($r as $key => $value) {
			if(empty($value))
				$r[$key] = '-';
		}
		return $r;
	}
}